package com.epfresh.supplier.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.epfresh.api.global.BaseFragment;
import com.epfresh.supplier.R;
import com.epfresh.supplier.constant.Constant;

/**
 * Created by zzq on 2017/12/4.
 */
public class AgentMyFragment extends BaseFragment {

    private TextView tvAgentMyAgent,tvAgentMyApply;

    public AgentMyRecordFragment recordFragment = new AgentMyRecordFragment();
    public AgentMyApplyFragment applyFragment = new AgentMyApplyFragment();
    FragmentTransaction transaction;
    FragmentManager manager;

    @Override
    public String getBaseTag() {
        return Constant.TAG_HOME_FRAGMENT;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_agent_my,
                container, false);
    }
    @Override
    public void onViewCreated(View v, Bundle savedInstanceState) {
        super.onViewCreated(v, savedInstanceState);
        tvAgentMyAgent = v.findViewById(R.id.tv_agent_my_agent);
        tvAgentMyApply = v.findViewById(R.id.tv_agent_my_apply);
        tvAgentMyApply.setOnClickListener(this);
        tvAgentMyAgent.setOnClickListener(this);
        manager = getFragmentManager();
        transaction = manager.beginTransaction();
        transaction.add(R.id.fragment_my_record, recordFragment,"MyAGENT");
        transaction.add(R.id.fragment_my_record, applyFragment,"MAKEAGENT");
        hideFragment();
        transaction.show(recordFragment).commit();
    }

    //隐藏所有的fragment
    private void hideFragment(){
        if(transaction!=null&& recordFragment != null){
            transaction.hide(recordFragment);
        }
        if(transaction!=null&& applyFragment != null){
            transaction.hide(applyFragment);
        }
    }

    private void showFragment(Fragment fragment){
        transaction = manager.beginTransaction();
        if(fragment==null) {
            if (fragment instanceof AgentMyApplyFragment) {
                fragment = new AgentMyApplyFragment();
                transaction.add(R.id.fragment_my_record,fragment,"MakeGENT");
            } else {
                fragment = new AgentMyRecordFragment();
                transaction.add(R.id.fragment_my_record,fragment,"MyAGENT");
            }
        }
        hideFragment();
        Log.e("____null________", fragment.toString());

        transaction.show(fragment);
        transaction.commit();
    }
    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.tv_agent_my_agent:
                tvAgentMyAgent.setTextColor(Color.RED);
                tvAgentMyApply.setTextColor(Color.parseColor("#999999"));
                showFragment(recordFragment);

                break;
            case R.id.tv_agent_my_apply:
                tvAgentMyApply.setTextColor(Color.RED);
                tvAgentMyAgent.setTextColor(Color.parseColor("#999999"));
                showFragment(applyFragment);

                break;
        }

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 0x002){// && resultCode == 0x001) {
           // initNotice();
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
